"""
读取故障信息采集卡的内容
@Time: 2021/2/5 19:49
@Author:
@File: fault_collected_card_scanner.py
"""
from fastapi import UploadFile
from typing import List, Tuple

import config
from models.dto.mark_dto.file_upload_dto import UploadResult
from models.normal.card_data_model import FaultInfoCardDataModel
from .base_scanner import CardScannerBase


from faker import Faker

fake = Faker(locale='zh_CN')


class FaultCollectedCardScanner(CardScannerBase):
    """用来读取故障信息采集卡的类"""
    def __init__(self):
        super().__init__()

    async def extract_data(self, card_file: UploadFile) -> FaultInfoCardDataModel:
        """
        读取传入的卡片文件，并返回读取的数据
        :param card_file: 卡片文件
        :return: 读取后的数据
        :raise: ScannerReadException 当读取错误时抛出此异常
        """
        return FaultInfoCardDataModel(brief=fake.sentence(),
                                      data_source_type=1,  # 表示数据源类型为故障信息卡
                                      fault_name=fake.name(),
                                      fault_time=fake.date_time_this_year(),
                                      fault_site=fake.address())

    async def save(self, card_file: UploadFile,
                   dir_name: str) -> None:
        """
        保存传入的卡片文件
        :param card_file: 卡片文件
        :raise: ScannerSaveException 当文件保存错误时抛出此异常
        """
        await super().save(card_file, dir_name)

    async def save_and_extract_all_files(self, card_files: List[UploadFile],
                                         dir_name: str = config.upload_conf.FMEA_CARD_FILE_PATH)\
            -> Tuple[List[FaultInfoCardDataModel], UploadResult]:
        return await super().save_and_extract_all_files(card_files, dir_name)

